//
//  RechargeCenter.m
//  qunfa
//
//  Created by Admin on 2017/8/15.
//  Copyright © 2017年 cuixiaocun. All rights reserved.
//

#import "RechargeCenter.h"

@interface RechargeCenter ()<CXCNavgationDelegate>
{
    UILabel *yueLabel;
    UILabel *payLabel;
    NSMutableArray *inforArr;
    NSString *monthString;
    NSString *amountString;
    UITextField *noteTF;
}
@end
@implementation RechargeCenter

- (void)viewDidLoad {

    [super viewDidLoad];
    [self.view setBackgroundColor:BGColor];
    NavgationView *nav =[[NavgationView alloc]initWithFrame:CGRectMake(0, 0, CXCWidth, 64) with:@"充值中心" isBackBtn:@"YES" rightBtnName:@"NO"];
    [self.view addSubview:nav];
    nav.delegate=self;
    inforArr =[[NSMutableArray alloc]init];
    self.view.backgroundColor =BGColor;
    [self getInfor];
    [self mainView];
}
-(void)retunButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)getInfor{
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setDictionary:@{
                          @"memberId":[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]]] ,
                          }
     ];
    
    [PublicMethod AFNetworkPOSTurl:@"index.php/Home/Member/memberInfo" paraments:dic1  addView:self.view success:^(id responseDic) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
        if ([[NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"]) 
        {
         yueLabel.text=[[[dict objectForKey:@"data"] objectForKey:@"member"] objectForKey:@"endtime"];
    
        
        }
        
    } fail:^(NSError *error) {
        
    }];


}
- (void)mainView
{

    //底部scrollview
    TPKeyboardAvoidingScrollView *bgScrollView =[[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 64+20*Width,CXCWidth, CXCHeight-64-20*Width )];
    [bgScrollView setUserInteractionEnabled:YES];
    [bgScrollView setBackgroundColor:BGColor];
    [self.view addSubview:bgScrollView];
    bgScrollView.showsVerticalScrollIndicator =NO;

    
    UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CXCWidth, 240*Width)];
    [bgView setBackgroundColor:NavColor];
    [bgScrollView addSubview:bgView];
    
    UILabel * yuePromLabel = [[UILabel alloc]initWithFrame:CGRectMake(30*Width, 0,600*Width, 100*Width)];
    yuePromLabel.text =@"到期日期";
    yuePromLabel.textColor =[UIColor whiteColor];
    yuePromLabel.font =[UIFont systemFontOfSize:14];
    [bgView addSubview:yuePromLabel];
    
    yueLabel =[[UILabel alloc]initWithFrame:CGRectMake(30*Width, yuePromLabel.bottom, 600*Width, 140*Width)];
    yueLabel.text =@"2018-05-19";
    yueLabel.textColor =[UIColor whiteColor];
    yueLabel.font =[UIFont systemFontOfSize:24];
    [bgView addSubview:yueLabel];
    
    UIButton *payBtn =[[UIButton alloc]initWithFrame:CGRectMake(0, bgView.bottom, CXCWidth, 100*Width)];
    [payBtn addTarget:self action:@selector(payBtnAction) forControlEvents:UIControlEventTouchUpInside];
    payBtn.backgroundColor =[UIColor whiteColor];
    [bgScrollView addSubview:payBtn];
    
    UIImageView *rmbImgV=[[UIImageView alloc]initWithFrame:CGRectMake(30*Width, 25*Width, 50*Width,50*Width )];
    rmbImgV.image =[UIImage imageNamed:@"icon_chongzhi_jine"];
    [payBtn addSubview:rmbImgV];
    
    UILabel *promPayLabel =[[UILabel alloc]initWithFrame:CGRectMake(rmbImgV.right+30*Width, 0,300*Width ,100*Width )];
    promPayLabel.textColor =TextColor;
    promPayLabel.text =@"充值金额";
    promPayLabel.font =[UIFont systemFontOfSize:16];
    [payBtn addSubview:promPayLabel];

    payLabel  =[[UILabel alloc]initWithFrame:CGRectMake(400*Width, 0, 320*Width, 100*Width)];
    payLabel.text =@"请选择";
    payLabel.textColor =TextColor;
    payLabel.textAlignment =NSTextAlignmentRight;
    payLabel.font =[UIFont systemFontOfSize:16];
    [payBtn addSubview:payLabel];

    UIButton *addAddressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addAddressBtn setFrame:CGRectMake(40*Width,payBtn.bottom+100*Width , 670*Width, 88*Width)];
    [addAddressBtn setBackgroundColor:NavColor];
    [addAddressBtn.layer setCornerRadius:4];
    [addAddressBtn.layer setMasksToBounds:YES];
    
    [addAddressBtn setTitle:@"确认充值" forState:UIControlStateNormal];
    [addAddressBtn.titleLabel setTextColor:[UIColor whiteColor]];
    [addAddressBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [addAddressBtn addTarget:self action:@selector(payTureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:addAddressBtn];
    
    
}
- (void)payTureBtnAction
{
    if([payLabel.text isEqualToString:@"请选择"])
    {
        [MBProgressHUD showSuccess:@"请选择充值金额" ToView:self.view];
        return;
    }

    [self ProPayRequest];

}

- (void)payBtnAction
{
    
    [PublicMethod AFNetworkGETurl:@"index.php/Home/Recharge/rechargeWx" paraments:@{@"memberId":[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]]} addView:self.view success:^(id responseDic) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
        NSString *str =[[NSString alloc] initWithData:responseDic  encoding:NSUTF8StringEncoding];
        NSError *parseError = nil;
        if ([[NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"]){
            NSArray *arr =[dict objectForKey:@"data"];
            inforArr =[[NSMutableArray alloc]init];
            for (int i=0; i<arr.count; i++) {
                [inforArr addObject:[NSString stringWithFormat:@"%@个月  共%@元",[arr[i] objectForKey:@"month"],[arr[i] objectForKey:@"money"]]];

            }
            //选择
            SRActionSheet *actionSheet = [SRActionSheet sr_actionSheetViewWithTitle:@"选择推广范围" cancelTitle:@"取消" destructiveTitle:nil withNumber:[NSString stringWithFormat:@"%ld",inforArr.count+1] withLineNumber:@"1" otherTitles:inforArr otherImages:nil selectActionBlock:^(SRActionSheet *actionSheet, NSInteger index) {
                if (index<0||index>arr.count-1) {
                    return;
                }
                payLabel.text =[NSString stringWithFormat:@"%@",[arr[index] objectForKey:@"money"]];
                NSLog(@"%ld----%@", index,payLabel.text);
                monthString =[arr[index] objectForKey:@"month"];
                amountString =[arr[index] objectForKey:@"money"];
            }];
            
            [actionSheet show];
        }
                                                                                  
    } fail:^(NSError *error) {
        
        }];
    
   }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)ProPayRequest
{

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"\n请将费用支付到如下账户\n微信：13345098353\n或支付宝:13345098353" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"我已转账" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [noteTF.text isEqualToString:@""];
        NSLog(@"%@",noteTF.text);
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        [dic1 setDictionary:@{
                              @"memberId":[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]],
                              @"amount":[NSString stringWithFormat:@"%@",amountString],
                              @"month":[NSString stringWithFormat:@"%@",monthString],
                              @"memberAccount":[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"account"]],
                              @"note":[NSString stringWithFormat:@"%@",noteTF.text],
                              
                              }];
            [PublicMethod AFNetworkPOSTurl:@"index.php/Home/Recharge/recharge" paraments:dic1  addView:self.view success:^(id responseDic) {
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
                if ([ [NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"]) {
                    [MBProgressHUD  showSuccess:[NSString stringWithFormat:@"%@",[dict objectForKey:@"data"]] ToView:self.view];
                }
                
            } fail:^(NSError *error) {
                
            }];
        
        
        
        NSLog(@"点击确认");
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"我未转账" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"点击取消");
        
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder =@"备注";
        NSLog(@"添加一个textField就会调用 这个block");
        textField.tag =1122;
        noteTF =textField;
        
    }];
    

    [self presentViewController:alertController animated:YES completion:nil];
    

    
}//:(NSNotification *)payNotificat
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
